package com.ubs.training.server.dao.impl.jdbc;

import com.ubs.training.server.dao.ProductDao;
import com.ubs.training.server.dao.model.Product;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import javax.sql.DataSource;
import java.util.Collections;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xubuntu
 * Date: 8/21/12
 * Time: 2:34 AM
 * To change this template use File | Settings | File Templates.
 */
public class NamedParamProductDaoImpl implements ProductDao {

    DataSource ds;
    NamedParameterJdbcOperations ops;

    public DataSource getDs() {
        return ds;
    }

    public void setDs(DataSource ds) {
        this.ds = ds;
        ops = new NamedParameterJdbcTemplate(ds);
    }

    @Override
    public List<Product> getAll() {
        return ops.query("select id_product, name_product, start_date, status, type_product, country, clientId from products", Collections.EMPTY_MAP, new BeanPropertyRowMapper<Product>(Product.class));
    }

    @Override
    public Product getById(int id) {
        return ops.queryForObject("select id_product, name_product, start_date, status, type_product, country, clientId from products where id_product=:productID", Collections.singletonMap("productID", id), new BeanPropertyRowMapper<Product>(Product.class));
    }

    @Override
    public boolean add(Product product) {
        int id = ops.queryForInt("select next value for cmstr_seq from dual", Collections.EMPTY_MAP);
        product.setId_product(id);
        return ops.update("insert into products values (:id_product, :name_product, :start_date, :status, :type_product, :country, :clientId)", new BeanPropertySqlParameterSource(product))>0;
    }

    @Override
    public boolean update(Product product) {
        int into_id = product.getId_product();
        Product old_product = ops.queryForObject("select id_product, name_product, start_date, status, type_product, country, clientId from products where id_product=:productID", Collections.singletonMap("productID", into_id), new BeanPropertyRowMapper<Product>(Product.class));
        if (!product.equals(old_product)) {
           return ops.update("update products set name_product=:name_product, start_date=:start_date, status=:status, type_product=:type_product, country=:country, clientId=:clientId where id_product=:id_product ", new BeanPropertySqlParameterSource(product))>0;
        }
        return false;
    }

    @Override
    public boolean remove(Product product) {
        return ops.update("delete from products where id_product=:id_product", new BeanPropertySqlParameterSource(product))>0;
    }

    @Override
    public List<Product> getByClientId(int id) {
        return ops.query("select id_product, name_product, start_date, status, type_product, country, clientId from products where clientId=:clientId", Collections.singletonMap("clientId", id), new BeanPropertyRowMapper<Product>(Product.class));
    }
}
